<?php

namespace app\admin\controller;

use app\model\AdminModel;
use think\App;

class Login extends Base
{

    private $req;
    private $adminModel;

    private $freeMinute = 5; //失败5次冻结时间

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->adminModel  = new AdminModel();
    }

    /*
     * 登录账号校验
     *
     * */
    public function Login(){

        if(!$this->req['username'] || !$this->req['password']){
            $this->fail("账号密码不能为空");
        }


        $first = $this->adminModel->getInfo($this->req['username'] );

        if(!$first){
            return json(['code' => 0, 'msg' => '账号不存在']);
        }


        if($first['status'] == -1){
            return json(['code' => 0, 'msg' => '账号被冻结，请联系客服']);
        }

        if($first['password'] != $this->req['password']){
            return json(['code' => 0, 'msg' => '账号密码错误']);
        }

        if($first){
            $res = $this->adminModel->setCache($first);
        }else{
            return json(['code' => 0, 'msg' => '登录失败']);
        }

        if($res){
            return $this->succ(['token'=>$first['token'],"登录成功！"]);
        }else{
            return $this->fail("登录失败");
        }
    }


    public function logout(){

        $token = request()->header('token');

        $res = $this->adminModel->delCahce($token);

        if($res){
            return $this->succ("退出成功！");
        }else{
            return $this->fail("登录失败");
        }
    }
}
